import * as cc from "cc";
import { BackgroundAdapter } from "../adapter/BackgroundAdapter";
import ContentAdapter from "../adapter/ContentAdapter";
import BaseComponent from "../components/BaseComponent";
import { CompositeDisposable } from "../event/EventKit";
import { GameEventDefine } from "../event/GameEventDefine";
import { IEventHandler } from "../event/IEventHandler";
import { Loger } from "../loger/Loger";
import NodeUtils from "../utils/NodeUtils";
import { ViewLayer, ViewLayerSort } from "../view/ViewConsts";
import SceneMgr from "./SceneMgr";

const { ccclass, property, menu } = cc._decorator;
@ccclass
@menu("GameScene/SceneBase")
export default class SceneBase extends BaseComponent implements IEventHandler {
    public disposable: CompositeDisposable = null;
    protected _sceneName: string = "SceneBase";
    public get sceneName() { return this._sceneName }
    protected _canvas: cc.Canvas = null
    protected _mainCamera: cc.Camera = null
    protected _sceneEffectRoot: cc.Node = null          //场景特效根节点,高级层级
    protected _uiRoot: { [key: number]: cc.Node } = {}
    protected _isLongScreen: boolean = false;
    protected _blockNode:cc.Node = null;
    public get isLongScreen(): boolean { return this._isLongScreen }
    public get mainCamera(): cc.Camera { return this._mainCamera }
    public get canvas(): cc.Canvas { return this._canvas }

    protected onLoad() {
        super.onLoad();
        Loger.log(`${cc.director.getScene().name} onLoad`);
        this._sceneName = cc.director.getScene().name;
        this._canvas = this.node.getComponent(cc.Canvas)
        this._mainCamera = this._canvas.cameraComponent;

        this.initLongScreen();
        this.initUIRoot()
        this.initBlock();
        SceneMgr.getInstance().setCurScene(this)
    }

    start() {
        this.registEvent();
    }

    /**注册事件监听 */
    registEvent(): void {
        //子类重写
        this.disposable = new CompositeDisposable();
        this.disposable.add(GameEventDefine.ON_SHOW_BLOCK.on(this.onBlockShow.bind(this)));
    }

    onBlockShow(isShow:boolean){
        this._blockNode.active = isShow;
    }

    initBlock(){
        if (!this._blockNode) {
            this._blockNode = new cc.Node();
            this._blockNode.name = "BlockNode";
            this._blockNode.addComponent(cc.UITransform);
            this._blockNode.addComponent(cc.BlockInputEvents);                        
            this._blockNode.addComponent(BackgroundAdapter);
            this._blockNode.setParent(this.node);
            this.node.setSiblingIndex(Number.MAX_VALUE);
        }
        this.onBlockShow(false);
    }

    /**初始化UI根节点 */
    initUIRoot() {
        this._uiRoot = {}
        for (let i = 0; i < ViewLayerSort.length; i++) {
            const layer = ViewLayerSort[i];
            let uiRoot = NodeUtils.getOrAddChildNode(this.node, layer)
            let uiTrans = NodeUtils.getOrAddComponent<cc.UITransform>(uiRoot, cc.UITransform)
            uiTrans.setContentSize(this.uiTrans.contentSize)
            NodeUtils.getOrAddComponent(uiRoot, ContentAdapter)
            uiRoot.setSiblingIndex(i + 20)
            this._uiRoot[layer] = uiRoot
        }
    }

    public getUIRoot(rootName: ViewLayer): cc.Node {
        return this._uiRoot[rootName]
    }

    init(id: number) { }

    // update (dt) {}
    /**是否长屏手机 */
    initLongScreen() {
        let uiTrans = this.node.getComponent(cc.UITransform);
        // 1. 先找到 SHOW_ALL 模式适配之后，本节点的实际宽高以及初始缩放值
        let srcScaleForShowAll = Math.min(cc.screen.windowSize.width / uiTrans.width, cc.screen.windowSize.height / uiTrans.height);
        let realWidth = uiTrans.width * srcScaleForShowAll;
        let realHeight = uiTrans.height * srcScaleForShowAll;

        // 2. 基于第一步的数据，再做节点宽高适配
        let width = uiTrans.width * (cc.screen.windowSize.width / realWidth);
        let height = uiTrans.height * (cc.screen.windowSize.height / realHeight);


        this._isLongScreen = (width / height) < 0.5;
    }

    onDestroy() {
        this.disposeEvent();
        super.onDestroy();
    }
}
